﻿using System;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using ChannelVN.Cached.Base;
using GUI.Extensions;

namespace GUI.Controls.News
{
    public partial class CareableNews : ControlBase
    {
        #region Overrides of ControlBase

        public override string CacheConfigName
        {
            get { return "Controls-News-CareableNews"; }
        }

        public override bool IsCachedChanged(long newsId, string zoneId, long tagId)
        {
            return !string.IsNullOrEmpty(zoneId);
        }

        #endregion

        const int Top = 24;
        const int ImageWidth = 196;
        const int ImageHeight = 133;
        const string Format = "<div class=\"item\">{3}<br /><p><a href=\"{0}\" title=\"{2}\">{1}</a></p></div>";
        private readonly  static bool EnableCacheHtml = true && CacheSettingExtensions.EnableCacheHtml;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Visible = false;
            //return;
            var bo = EnableCacheHtml ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();
            var dt = bo.GetCareableNews(Top);

            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new StringBuilder();
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];

                    var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth, ImageHeight, lazyLoad: true);

                    if (i % 6 == 0)
                    {
                        sb.Append("<li>");
                    }
                    sb.AppendFormat(
                            Format,
                            r[SqlConstants.NewsPublish.Url],
                            r[SqlConstants.NewsPublish.Title],
                            PresentationUtils.EnCode(r[SqlConstants.NewsPublish.Title].ToString()),
                            avatar
                        );

                    if ((i + 1) % 3 == 0)
                    {
                        sb.Append("<div style=\"clear:both\"></div>");
                    }

                    if ((i + 1) % 6 == 0 || i == dt.Rows.Count)
                    {
                        sb.Append("<div style=\"clear:both\"></div></li>");
                    }
                }
                ltrBoxCare.Text = sb.ToString();
                return;
            }
            Visible = false;
        }
    }
}
